package com.le.tester.imooc.masterJUC.lock.atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * 描述：性能和LongAdder差不多，但是方法更加的通用
 */
public class LongAccumulatorDemo {
    public static void main(String[] args) {
        LongAccumulator accumulator = new LongAccumulator((x, y) -> x * y, 0);
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        IntStream.range(1, 10).forEach(i -> executorService.submit(() -> accumulator.accumulate(i)));
        //线程记得shutDown()
        executorService.shutdown();
        while (!executorService.isTerminated()) {
        }

        System.out.println(accumulator.getThenReset());
    }
}
